/**
 * 
 */
package userController;


import java.sql.SQLException;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;

import java.net.URL;
import java.util.Date;
import java.util.Map;
import java.util.ResourceBundle;

import paymentController.PaymentController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import userEntity.MembershipEntity;
import userEntity.ViewMembershipEntity;
import userLogic.MembershipLogic;
import vehicleEntity.PurchasedVehicleDisplay;
import vehicleEntity.PurchasedVehicleEntity;
import vehicleEntity.SaleVehicleEntity;
import vehicleEntity.VehicleDisplayable;
import vehicleLogic.SaleLogic;
import userLogic.MembershipLogic;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 * @author Yokesh Kumar
 *
 */

public class MembershipController implements Initializable{
	
    //Start of Table columns FXML Values
	@FXML
	TableColumn<ViewMembershipEntity, Integer>	rentid_fxid;
	
	@FXML
	TableColumn<ViewMembershipEntity, Date> rentDate_fxid;
	@FXML
	TableColumn<ViewMembershipEntity, Date> returnDate_fxid;
	@FXML
	TableColumn<ViewMembershipEntity, String> vehicleName_fxid;
	@FXML
	TableColumn<ViewMembershipEntity, String> vehicleType_fxid;
	@FXML
	TableColumn<ViewMembershipEntity, Integer> pointsRedeemed_fxid;
	@FXML
	TableColumn<ViewMembershipEntity, Integer> pointsGained_fxid;
	
	
	@FXML
	private TableView<ViewMembershipEntity> pointsTable_fxid;
	//end of Table Column FXML values
	
	//for view membership
	private static ObservableList<ViewMembershipEntity> membershipList;
	//end
	
	@FXML
	private TextArea cancelDesc;
	@FXML
	private Label memberID;
	@FXML
	private Label joinDate;
	@FXML
	private Label points;
	@FXML
	private Label expirydate;
	@FXML
	private Label message;
	@FXML
	private Label expirydays;
	

	
	private static MembershipEntity membership;

	
	
	/**
	 * @return the membership
	 */
	public static MembershipEntity getMembership() {
		return membership;
	}

	/**
	 * @param membership the membership to set
	 */
	public static void setMembership(MembershipEntity membership) {
		MembershipController.membership = membership;
	}

	@FXML
	public void createMembership(ActionEvent event){
		Navigator.loadStackPane(Navigator.MEMBERSHIPPAYMENTFXML);
	}
	
	@FXML
	public void cancelMembership(ActionEvent event){
		Navigator.loadStackPane(Navigator.CANCELMEMBERSHIPFXML);
	}
	
	@FXML
	public void cancelMembershipConfirm(ActionEvent event){
		String msg = "";
		if(cancelDesc != null && !cancelDesc.getText().isEmpty()){
			MembershipLogic logic = new MembershipLogic();
			msg = logic.cancelMemberShip(membership.getMemberShipId(), cancelDesc.getText());
			message.setText(msg);
			membership = null;
			Navigator.loadStackPane(Navigator.BecomeAMemberFXML);
			
		}
		else{
			message.setText("Please enter the reason for Membership cancellation");
		}
		
	}
	
	@FXML
	public void renewMembership(ActionEvent event){
		// load payment controller- set payment for to renewMembership
							//		set payment type to credit card
		// pass on membershipId and PhoneNumber
		System.out.println("Inside renew membership");
		Navigator.loadStackPane(Navigator.MEMBERSHIPRENEWALPAYMENTFXML);		
	}
	
	/**
	 * This method displays the membership table and point history values of a member
	 * @param event
	 * @Pre Action event passed from the Customer Controller class 
	 * @Post membershipId retrieved from MembershipEntity class is then passed to MembershipLogic class
	 * @throws SQLException
	 */
	// View Membership ************************************* calling view membership table
	@FXML
	public void viewMembershipPointHistory(ActionEvent event) throws SQLException{
		//fetchMembershipPointsHistory()  -- have corrected the spelling mistake "points" instead of "ponits"
		MembershipLogic memberlog = new MembershipLogic();
		membershipList=FXCollections.observableArrayList();
		membershipList = memberlog.fetchMembershipPointsHistory(membership.getMemberShipId());
		System.out.println("Membership ID retrieved is "+membership.getMemberShipId());
		System.out.println("View membership invoked");
		Navigator.loadStackPane(Navigator.VIEWMEMBERSHIPFXML);
	}

	@FXML
	public void backToMembership(ActionEvent event){
		Navigator.setInitailize(true);
		Navigator.loadStackPane(Navigator.MEMBERSHIPFXML);
	}
	/**
	 * @param membership
	 */
	public void showMembershipDetails(MembershipEntity memberEntity) {
		MembershipLogic logic = new MembershipLogic();
		if(memberEntity != null){
			setMembership(memberEntity);
			logic.checkForExpiry(membership);
			Navigator.setInitailize(true);
			Navigator.loadStackPane(Navigator.MEMBERSHIPFXML);
		}
		else{
			System.out.println("showMembershipDetails Method: membership entity null");
		}
		
		
	}

	/* (non-Javadoc)
	 * @see javafx.fxml.Initializable#initialize(java.net.URL, java.util.ResourceBundle)
	 */
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		if(membership != null && Navigator.isInitailize()){
			MembershipLogic logic = new MembershipLogic();
			memberID.setText(membership.getMemberShipId().toString());
			points.setText(membership.getPoints().toString());
			joinDate.setText(membership.getJoinDate().toString());
			expirydate.setText(membership.getExpirydate().toString());
			expirydays.setText(logic.calculateExpiryDays(membership));
			Navigator.setInitailize(false);
		}
		
		if(!Navigator.isInitailize() && membershipList != null && membershipList.size() >0){
			System.out.println(membershipList.size());
			rentid_fxid.setCellValueFactory(new PropertyValueFactory<ViewMembershipEntity, Integer>("rentId"));
			rentDate_fxid.setCellValueFactory(new PropertyValueFactory<ViewMembershipEntity, Date>("rentDate"));
			returnDate_fxid.setCellValueFactory(new PropertyValueFactory<ViewMembershipEntity, Date>("returnDate"));
			vehicleName_fxid.setCellValueFactory(new PropertyValueFactory<ViewMembershipEntity, String>("vehicleName"));
			vehicleType_fxid.setCellValueFactory(new PropertyValueFactory<ViewMembershipEntity, String>("vehicleType"));
			pointsRedeemed_fxid.setCellValueFactory(new PropertyValueFactory<ViewMembershipEntity, Integer>("pointsRedeemed"));
			pointsGained_fxid.setCellValueFactory(new PropertyValueFactory<ViewMembershipEntity, Integer>("pointsGained"));
			pointsTable_fxid.getItems().setAll(membershipList);
			Navigator.setInitailize(false);
			
			
			
		}
		
	}
	
	
}
